package com.zxh;

import com.zxh.bean.ConsumerComponent;
import com.zxh.utils.TP9099Report;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ConsumerApplication {


    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();

        //创建线程池进行调用服务
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        /***
         * 创建线程池并行调用3个方法
         */
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(6);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            service.methodA();
        }, 5, 60, TimeUnit.MILLISECONDS);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            service.methodB();
        }, 5, 60, TimeUnit.MILLISECONDS);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            service.methodC();
        }, 5, 60, TimeUnit.MILLISECONDS);
        //每隔5s打印一次最近1分钟内每个方法的TP90、TP99的耗时情况
        Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(new TP9099Report(), 5,5, TimeUnit.SECONDS);
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.zxh.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
